package com.jason.apps.dashboard;

import sample.classes.ActionItem;
import sample.classes.QuickAction;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.apps.realtyportal.HomeActivity;
import com.apps.realtyportal.MyFragmentPagerAdapterManageReports;
import com.apps.realtyportal.R;
import com.ipaulpro.afilechooser.utils.FileUtils;

public class Reports extends SherlockFragmentActivity {

	public static Context appContext;
	ActionBar actionbar;

	private static final int REQUEST_CODE = 1;
	private static final int REQUEST_EDIT = 2;
	private static final int ID_UP = 1;
	private static final int ID_DOWN = 2;

	FragmentManager fm;

	ViewPager mPager;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.agent_fragment);
		appContext = getApplicationContext();

		// ActionBar
		actionbar = getSupportActionBar();
		actionbar.setDisplayHomeAsUpEnabled(true);
		actionbar.setDisplayShowTitleEnabled(false);

		/** Getting a reference to action bar of this activity */
		// actionbar = getSupportActionBar();

		/** Set tab navigation mode */
		actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		/** Getting a reference to ViewPager from the layout */
		mPager = (ViewPager) findViewById(R.id.pager);

		/** Getting a reference to FragmentManager */
		fm = getSupportFragmentManager();

		/** Defining a listener for pageChange */
		ViewPager.SimpleOnPageChangeListener pageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
			@Override
			public void onPageSelected(int position) {
				super.onPageSelected(position);
				actionbar.setSelectedNavigationItem(position);
			}

		};

		/** Setting the pageChange listner to the viewPager */
		mPager.setOnPageChangeListener(pageChangeListener);

		/** Creating an instance of FragmentPagerAdapter */
		MyFragmentPagerAdapterManageReports fragmentPagerAdapter = new MyFragmentPagerAdapterManageReports(
				fm);

		/** Setting the FragmentPagerAdapter object to the viewPager object */
		mPager.setAdapter(fragmentPagerAdapter);

		actionbar.setDisplayShowTitleEnabled(true);

		/** Defining tab listener */
		ActionBar.TabListener tabListener = new ActionBar.TabListener() {
			public Fragment fragment;

			public void onTabUnselected(Tab tab, FragmentTransaction ft) {
			}

			public void onTabSelected(Tab tab, FragmentTransaction ft) {
				mPager.setCurrentItem(tab.getPosition());
				// ft.replace(R.id.buyer_fragment, fragment);
			}

			public void onTabReselected(Tab tab, FragmentTransaction ft) {

				fm.popBackStack();
			}
		};

		/** Creating Android Tab */
		Tab tab = actionbar.newTab().setText("Sold")
				.setIcon(R.drawable.soldxml).setTabListener(tabListener);

		actionbar.addTab(tab);

		/** Creating Apple Tab */
		tab = actionbar.newTab().setText("Chart").setIcon(R.drawable.chartxml)
				.setTabListener(tabListener);

		actionbar.addTab(tab);

		// actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		//
		// ActionBar.Tab latest = actionbar.newTab().setText("All");
		// ActionBar.Tab chart = actionbar.newTab().setText("Chart");
		//
		// Fragment LatestFragment = new LatestSold();
		// Fragment ChartFragment = new ShowChart();
		// // Fragment DeleteFragment = new DeleteAccount();
		//
		// latest.setTabListener(new MyTabsListener(LatestFragment));
		// chart.setTabListener(new MyTabsListener(ChartFragment));
		// // delete.setTabListener(new MyTabsListener(DeleteFragment));
		//
		// actionbar.addTab(latest);
		// actionbar.addTab(chart);
		// // actionbar.addTab(delete);

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			Intent intent = new Intent(this, HomeActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
			startActivity(intent);

		case R.id.logoutActionBar:

			ActionItem nextItem = new ActionItem(ID_DOWN, "Go To Dashboard",
					getResources().getDrawable(R.drawable.menu_down_arrow));
			ActionItem prevItem = new ActionItem(ID_UP, "Log out",
					getResources().getDrawable(R.drawable.menu_up_arrow));

			// use setSticky(true) to disable QuickAction dialog being
			// dismissed
			// after an item is clicked
			prevItem.setSticky(true);
			nextItem.setSticky(true);
			final QuickAction quickAction1 = new QuickAction(
					getApplicationContext(), QuickAction.VERTICAL);
			// add action items into QuickAction
			quickAction1.addActionItem(nextItem);
			quickAction1.addActionItem(prevItem);

			// Set listener for action item clicked
			quickAction1
					.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
						public void onItemClick(QuickAction source, int pos,
								int actionId) {
							ActionItem actionItem = quickAction1
									.getActionItem(pos);
							// here we can filter which action item was
							// clicked with
							// pos or actionId parameter
							if (actionId == ID_DOWN) {
								Toast.makeText(getApplicationContext(),
										"Go to Dashboard", Toast.LENGTH_SHORT)
										.show();
								finishActivity(0);
								startActivity(new Intent(
										getApplicationContext(),
										DashBoard.class));

							} else if (actionId == ID_UP) {
								Toast.makeText(getApplicationContext(),
										"Logging out", Toast.LENGTH_SHORT)
										.show();

								HomeActivity.islogin = false;
								finishActivity(0);
								startActivity(new Intent(
										getApplicationContext(),
										HomeActivity.class));

							} else {
								Toast.makeText(getApplicationContext(),
										actionItem.getTitle() + " selected",
										Toast.LENGTH_SHORT).show();
							}
						}
					});
			quickAction1
					.setOnDismissListener(new QuickAction.OnDismissListener() {
						public void onDismiss() {
							Toast.makeText(getApplicationContext(),
									"Dismissed", Toast.LENGTH_SHORT).show();
						}
					});

			quickAction1.show(mPager);
			quickAction1.setAnimStyle(QuickAction.ANIM_AUTO);

		}

		return super.onOptionsItemSelected(item);
	}

	public void Browse(View v) {
		Intent target = FileUtils.createGetContentIntent();
		Intent intent = Intent.createChooser(target,
				getString(R.string.chooser_title));

		try {
			startActivityForResult(intent, REQUEST_CODE);
		} catch (ActivityNotFoundException e) {
		}
	}

	public void EditBrowse(View v) {
		Intent target = FileUtils.createGetContentIntent();
		Intent intent = Intent.createChooser(target,
				getString(R.string.chooser_title));

		try {
			startActivityForResult(intent, REQUEST_EDIT);
		} catch (ActivityNotFoundException e) {
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.activity_home, menu);
		return true;
	}

	// @Override
	// public void onBackPressed() {
	// // TODO Auto-generated method stub
	// super.onBackPressed();
	// this.finish();
	// }


	@Override
	public void onBackPressed() {
		Toast.makeText(getApplicationContext(), "Please use the up button",
				Toast.LENGTH_LONG).show();
	}

	/*
	 * public boolean onOptionsItemSelected(MenuItem item) { switch
	 * (item.getItemId()) { case R.id.triger_year:
	 * //showyear.setVisibility(View.VISIBLE); ShowChart details =
	 * (ShowChart)getSupportFragmentManager
	 * ().findFragmentById(R.id.fragment_container); details.Show(); break;
	 * 
	 * default: break; }
	 * 
	 * return true; }
	 */

	class MyTabsListener implements ActionBar.TabListener {
		public Fragment fragment;

		public MyTabsListener(Fragment fragment) {
			this.fragment = fragment;
		}

		public void onTabReselected(Tab tab, FragmentTransaction ft) {
			FragmentManager fm = this.fragment.getFragmentManager();
			fm.popBackStack();
		}

		public void onTabSelected(Tab tab, FragmentTransaction ft) {
			ft.replace(R.id.agent_fragment, fragment);
		}

		public void onTabUnselected(Tab tab, FragmentTransaction ft) {
			// ft.detach(fragment);
		}
	}
}
